package com.edu.base.common.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 生成树结构数据
     * @ClassName: BuildTree
     * @Description: TODO(这里用一句话描述这个类的作用)
     * @author lzs
     * @date 2020年8月17日
     *
 */
public class BuildTree {

	private List<TreeNode> TreeNodes = new ArrayList<>();

	public BuildTree(List<TreeNode> TreeNodes) { 
		this.TreeNodes = TreeNodes;
	}

	public BuildTree() {
 	}

	public List<TreeNode> buildTree(List<TreeNode> TreeNodes) {
		BuildTree treeBuilder = new BuildTree(TreeNodes);
 		return treeBuilder.buildJSONTree();
	}

	// 构建JSON树形结构
	private List<TreeNode> buildJSONTree() {
		List<TreeNode> TreeNodeTree = buildTree();
		return TreeNodeTree;
	}

	// 构建树形结构
	private List<TreeNode> buildTree() {
		List<TreeNode> treeTreeNodes = new ArrayList<>();
		// 获取所有根节点
		List<TreeNode> rootTreeNodes = getRootTreeNodes();
		// 获取每个根节点
		for (TreeNode rootTreeNode : rootTreeNodes) {
			try {
				// 递归这个根节点的子节点
				buildChildTreeNodes(rootTreeNode);
				// 用list来存放每个根节点
				treeTreeNodes.add(rootTreeNode);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return treeTreeNodes;
	}

	// 递归子节点
	private void buildChildTreeNodes(TreeNode TreeNode) {
		List<TreeNode> children = getChildTreeNodes(TreeNode);
		if (!children.isEmpty()) {
			for (TreeNode child : children)
				try {
					buildChildTreeNodes(child);
				} catch (Exception e) {
					e.printStackTrace();
				}
			TreeNode.setChildren(children);
		}
	}

	/**
	 * 获取父节点下所有的子节点
	 * 
	 * @param pTreeNode
	 * @return
	 */
	private List<TreeNode> getChildTreeNodes(TreeNode pTreeNode) {
		List<TreeNode> childTreeNodes = new ArrayList<>();

		for (TreeNode n : TreeNodes) {
			if (pTreeNode.getCode().equals(n.getPcode())) {
				childTreeNodes.add(n);
			}
		}
		return childTreeNodes;
	}

	// 判断是否为根节点
	private boolean rootTreeNode(TreeNode TreeNode) {
		boolean isRootTreeNode = true;
		for (TreeNode n : TreeNodes) {
			if (TreeNode.getPcode().equals(n.getCode())) {
				isRootTreeNode = false;
				break;
			}
		}
		return isRootTreeNode;
	}

	// 获取集合中所有的根节点
	private List<TreeNode> getRootTreeNodes() {
		List<TreeNode> rootTreeNodes = new ArrayList<>();
		for (TreeNode n : TreeNodes) {
			if (rootTreeNode(n)) {
				rootTreeNodes.add(n);
			}
		}
		return rootTreeNodes;
	}
}
